<template>
	<!-- 直接修改密码 -->
	<view>
		<navBar title="修改密码"></navBar>
		
		<view class="form">
			<u-form :model="form" ref="uForm">
				<u-form-item label="密码" prop="pwd">
					<u-input placeholder="请输入密码" v-model="form.pwd" maxlength="15" trim />
				</u-form-item>
			</u-form>
		</view>

		<view class="btn">
			<u-button type="primary" @click="changePassword">修改</u-button>
		</view>


		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		changePassword,
		userLogout,
		changePassword2
	} from '../../utils/user.js'
	export default {
		data() {
			return {
				user_id: '',
				form: {
					pwd: ''
				},
				email: '',
				passwordType: '',

				rules: {
					pwd: [
						// 对密码字段进行长度验证
						{
							min: 8,
							message: '密码不能少于8个字',
							trigger: ['change', 'blur']
						},
						// 正则判断为字母或数字
						{
							pattern: /^[0-9a-zA-Z]*$/g,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '只能包含字母或数字!',
							trigger: ['change', 'blur']
						},
						// 对密码字段进行必填验证
						{
							required: true,
							message: '请填写密码',
							trigger: ['change', 'blur']
						},
					],
				}

			};
		},
		methods: {
			//用户退出
			async userLogouted() {
				let result = await userLogout(this.user_id)
				if (result.code === 200) {
					uni.removeStorageSync('token')
					uni.removeStorageSync('userInfo')
					this.$refs.uToast.show({
						title: '修改成功',
						type: 'success',
						duration: 1000,
						isTab: true,
						url: '/pages/center/center'
					})
				}
			},
			//修改密码			
			async changePassword() {
				this.$refs.uForm.validate(async (valid) => {
					if (valid) {
						if (this.passwordType === '1') {
							let data = {
								user_id: this.user_id,
								newpwd: this.form.pwd
							}
							let result = await changePassword(data)
							if (result.code === 200) {
								this.form.pwd = ''
								this.userLogouted()
							}else if(result.code === 515){
								this.$refs.uToast.show({
									title: '您的账号已被封禁，请联系开发者',
									type: 'error',
									duration: 1000
								})
							}else{
								this.$refs.uToast.show({
									title: '出现不知名错误',
									type: 'error',
									duration: 1000,
								})
							}
						} else {
							let data = {
								email: this.email,
								newpwd: this.form.pwd
							}
							let result = await changePassword2(data)
							if (result.code === 200) {
								this.$refs.uToast.show({
									title: '修改成功',
									type: 'success',
									duration: 1000,
									isTab: true,
									url: '/pages/center/center'
								})
							}else if(result.code === 515){
								this.$refs.uToast.show({
									title: '您的账号已被封禁，请联系开发者',
									type: 'error',
									duration: 1000
								})
							}else{
								this.$refs.uToast.show({
									title: '出现不知名错误',
									type: 'error',
									duration: 1000
								})
							}
						}
					}
				})
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(option) {
			this.user_id = uni.getStorageSync('userInfo').user_id
			this.passwordType = option.type
			this.email = option.email
		}
	}
</script>

<style lang="scss">
	.form {
		width: 60%;
		margin: 0 auto;
		margin-top: 400rpx;
		margin-bottom: 100rpx;
	}

	.btn {

		width: 30%;
		margin: 0 auto;
	}
</style>
